<?php
/*
Plugin Name: Simple Tags Ajax Extensions : Tooltips Tags
Plugin URI: http://wordpress.org/extend/plugins/simple-tags
Description: List posts for each tags with a roll over with a ToolTips
Version: 1.3
Author: Amaury BALMER
Author URI: http://www.herewithme.fr

&copy; Copyright 2007 Amaury BALMER (balmer.amaury@gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

Class ST_AjaxExtension_ToolTips {
	var $info;

	function ST_AjaxExtension_ToolTips() {
		// Determine installation path & url
		$path = basename(str_replace('\\','/',dirname(__FILE__)));

		$info['siteurl'] = get_option('siteurl');
		if ( strpos($path, 'mu-plugins') ) {
			$info['install_url'] = $info['siteurl'] . '/wp-content/mu-plugins';
			$info['install_dir'] = ABSPATH . 'wp-content/mu-plugins';

			if ( $path != 'mu-plugins' ) {
				$info['install_url'] .= '/' . $path;
				$info['install_dir'] .= '/' . $path;
			}
		} else {
			$info['install_url'] = $info['siteurl'] . '/wp-content/plugins';
			$info['install_dir'] = ABSPATH . 'wp-content/plugins';

			if ( $path != 'plugins' ) {
				$info['install_url'] .= '/' . $path;
				$info['install_dir'] .= '/' . $path;
			}
		}

		// Set informations
		$this->info = array(
			'siteurl' => $info['siteurl'],
			'install_url' => $info['install_url'],
			'install_dir' => $info['install_dir']
		);
		unset($info);

		// Check for ajax request
		add_action('init', array(&$this, 'checkAjaxRequest'));

		// Add JS and CSS
		add_action('wp_head', array(&$this, 'helperJS'), 99);

		// Add jQuery
		add_action('init', array(&$this, 'helperjQuery'));
	}

	function helperjQuery() {
		if ( !is_admin() ) {
			wp_enqueue_script('jquery');
		}
	}

	function helperJS() {
		?>
		<?php if ( !function_exists('wp_enqueue_script') ) : ?>
			<script src="<?php echo $this->info['siteurl']; ?>/wp-includes/js/jquery/jquery.js" type="text/javascript"></script>
		<?php endif; ?>

		<!-- Start of script generated by Simple Tags Ajax Extensions -->
		<script src="<?php echo $this->info['install_url']; ?>/js/jquery.dimensions.pack.js" type="text/javascript"></script>
		<script src="<?php echo $this->info['install_url']; ?>/js/jquery.hoverIntent.minified.js" type="text/javascript"></script>
		<script src="<?php echo $this->info['install_url']; ?>/js/jquery.cluetip.pack.js" type="text/javascript"></script>
		<link rel="stylesheet" href="<?php echo $this->info['install_url']; ?>/css/jquery.cluetip.css" type="text/css" />

		<script type="text/javascript">
		// <![CDATA[
			var quantity = 10; // Edit for post quantity
			
			function fixTargetAjax( url ) {						
				<?php
				$tag_base = get_option('tag_base');
				$tag_base = ( empty($tag_base) ) ? 'tag' : $tag_base;
				?>
				var pos = url.indexOf("<?php echo $tag_base; ?>");
				url= url.substr( pos + <?php echo strlen($tag_base); ?> + 1 );
				return '<?php echo $this->info['siteurl']; ?>/?st_ajax_action=get_posts_with_tag&qty='+quantity+'&tag=' + url;
			}

			jQuery(document).ready( function() {
				// The function cluetip() is modified for Simple Tags.
				// If attribute: 'href', the href value is filter with fixTargetAjax() function.
				jQuery('.tags_cloud a').cluetip({
					cluetipClass: 'jtip',
					arrows: true,
					dropShadow: false,
					hoverIntent: false,
					sticky: true,
					mouseOutClose: true,
					attribute: 'href',
					closePosition: 'title',
					closeText: '<img src="<?php echo $this->info['install_url']; ?>/images/cancel.png" alt="<?php echo attribute_escape(__('Close', 'simpletags')); ?>" />'
				});
			});
		// ]]>
		</script>
		<!-- End of script generated by Simple Tags Ajax Extensions -->
		<?php
	}

	function checkAjaxRequest() {
		if ( $_GET['st_ajax_action'] == 'get_posts_with_tag' ) {
			$this->getPostsByTags();
		}
	}

	function getPostsByTags() {
		$tag = attribute_escape(stripslashes($_GET['tag']));
		$tag = str_replace('/', '', $tag); // Remove unused slahes
		$qty = ( (int) $_GET['qty'] != 0 ) ? (int) $_GET['qty'] : 10;

		if ( empty($tag) ) {
			exit();
		}

		global $wpdb;
		$results = $wpdb->get_results("SELECT DISTINCT p.post_title, p.comment_count, p.post_date, p.ID, COUNT(tr.object_id) AS counter
				FROM {$wpdb->posts} AS p
				INNER JOIN {$wpdb->term_relationships} AS tr ON (p.ID = tr.object_id)
				INNER JOIN {$wpdb->term_taxonomy} AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
				INNER JOIN {$wpdb->terms} AS t ON (tt.term_id = t.term_id)
				WHERE tt.taxonomy = 'post_tag'
				AND t.slug = '{$tag}'
				AND p.post_status = 'publish'
				AND p.post_date < '".current_time('mysql')."'
				GROUP BY tr.object_id
				ORDER BY p.post_date DESC
				LIMIT 0, {$qty}");

		status_header( 200 );
		header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
		echo '<ul>';
		foreach ( (array) $results as $result ) {
			echo '<li><a href="'.get_permalink($result->ID).'" title="'.attribute_escape($result->post_title).'">'.attribute_escape($result->post_title).'</a></li>';
		}
		echo '</ul>';
		exit();
	}
}

if ( class_exists('SimpleTags') ) {
	$st_ae_tt = new ST_AjaxExtension_ToolTips();
}
?>